package com.china08.yxyapi.api.manage.tec.teaching.lesson.cloud;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudCatalog;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudCatalogRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemRepository;
import com.china08.yxyapi.service.lesson.TecCloudLessonService;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.StringTrimUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class TecCloudLessonListController extends YxyApiBaseController {

    @Autowired
    TecCloudLessonService service;

    @Autowired
    YxyLessonsCloudCatalogRepository catalogRepository;

    @Autowired
    YxyLessonsCloudLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lesson/cloud/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String keyword = reqModel.keyword;
        keyword = StringTrimUtil.trim(keyword);
        keyword = StringUtils.isNotBlank(keyword) ? "%" + keyword + "%" : "";
        String catalog1 = reqModel.catalog1;
        String catalog2 = reqModel.catalog2;
        String catalog3 = reqModel.catalog3;
        String catalog4 = reqModel.catalog4;
        String catalog5 = reqModel.catalog5;
        Integer pageNum = reqModel.pageNum;
        String schoolId = getProfile(request, response).getSchoolId();
        String authorId = getProfile(request, response).getId();
        String lessonStatus = reqModel.lessonStatus;
        String isJingPin = reqModel.isJingPin;
        Integer isJingpinLesson = -1;
        if (StringUtils.isNotBlank(isJingPin) && ("0".equals(isJingPin) || "1".equals(isJingPin))) {
            isJingpinLesson = Integer.parseInt(isJingPin);
        }
        Integer allPage = 0;
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = service.listForTec(schoolId, authorId, catalog1, catalog2, catalog3, catalog4, catalog5, lessonStatus, isJingpinLesson, keyword, pageNum);

        if (map != null) {
            allPage = Integer.parseInt(map.get("allPage") + "");
            list = (List<Map<String, Object>>) map.get("list");
        }

        List<Pojo> returnList = new ArrayList<Pojo>();
        for (Map<String, Object> m : list) {
            Pojo pojo = new Pojo();
            String lessonId = m.get("id") + "";
            pojo.id = lessonId;
            pojo.name = m.get("name") + "";
            pojo.cover = m.get("cover") + "";
            String c1 = m.get("catalog_id1") + "";
            String c2 = m.get("catalog_id2") + "";
            String c3 = m.get("catalog_id3") + "";
            String c4 = m.get("catalog_id4") + "";
            String c5 = m.get("catalog_id5") + "";

            // 获取栏目名称
            YxyLessonsCloudCatalog catalog = new YxyLessonsCloudCatalog();
            if (StringUtils.isNotBlank(c1) && !"null".equals(c1)) {
                catalog = catalogRepository.findById(c1);
                if (null != catalog) {
                    pojo.catalog1Name = catalog.getName();
                }
            }
            if (StringUtils.isNotBlank(c2) && !"null".equals(c2)) {
                catalog = catalogRepository.findById(c2);
                if (null != catalog) {
                    pojo.catalog2Name = catalog.getName();
                }
            }
            if (StringUtils.isNotBlank(c3) && !"null".equals(c3)) {
                catalog = catalogRepository.findById(c3);
                if (null != catalog) {
                    pojo.catalog3Name = catalog.getName();
                }
            }
            if (StringUtils.isNotBlank(c4) && !"null".equals(c4)) {
                catalog = catalogRepository.findById(c4);
                if (null != catalog) {
                    pojo.catalog4Name = catalog.getName();
                }
            }
            if (StringUtils.isNotBlank(c5) && !"null".equals(c5)) {
                catalog = catalogRepository.findById(c5);
                if (null != catalog) {
                    pojo.catalog5Name = catalog.getName();
                }
            }
            pojo.catalog1 = c1;
            pojo.catalog2 = c2;
            pojo.catalog3 = c3;
            pojo.catalog4 = c4;
            pojo.catalog5 = c5;

            Object obj0 = m.get("is_visible");
            Object obj1 = m.get("status");
            Object obj4 = m.get("up_Date");
            Object obj5 = m.get("create_date");
            Object obj6 = m.get("is_jingpin_lesson");
            if (obj0 != null) {
                pojo.isVisible = Integer.parseInt(obj0 + "");
            }
            if (null != obj1) {
                pojo.status = Integer.parseInt(obj1 + "");
            }
            if (null != obj4) {
                pojo.up_Date = DateToolUtils.format((Date) obj4, "yyyy-MM-dd");
            }
            if (null != obj5) {
                pojo.create_date = DateToolUtils.format((Date) obj5, "yyyy-MM-dd");
            }
            if (null != obj6) {
                pojo.isJingpinLesson = Integer.parseInt(obj6 + "");
            }

            // 获取课程的所有课时
            List<YxyLessonsCloudLessonItem> items = itemRepository.findByLessonIdAndStatusNot(lessonId, -1);
            // 总课时
            pojo.count = items.size();

            Integer status0Count = 0;
            Integer status1Count = 0;
            Integer status2Count = 0;
            Integer status3Count = 0;
            Integer duration = 0;
            for (YxyLessonsCloudLessonItem item : items) {
                Integer status = item.getStatus();
                if (status == 0) {
                    status0Count = status0Count + 1;
                }
                if (status == 1) {
                    status1Count = status1Count + 1;
                }
                if (status == 2) {
                    status2Count = status2Count + 1;
                }
                if (status == 3) {
                    status3Count = status3Count + 1;
                }
                // 获取视频id
                String vidoeId = item.getVideoId();
                YxyLessonsVideo video = videoRepository.findById(vidoeId);
                if (video != null) {
                    duration = duration + video.getDuration();
                }
            }
            pojo.status0Count = status0Count;
            pojo.status1Count = status1Count;
            pojo.status2Count = status2Count;
            pojo.status3Count = status3Count;
            pojo.duration = StringTrimUtil.disDuration(duration);
            returnList.add(pojo);
        }

        RespModel respModel = new RespModel();
        respModel.allPage = allPage;
        respModel.list = returnList;
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String catalog1;
        public String catalog2;
        public String catalog3;
        public String catalog4;
        public String catalog5;
        public String keyword;
        public Integer pageNum;
        public Integer leve;
        public String lessonStatus;
        public String isJingPin;
    }

    public static class Pojo {
        public String id;
        public String name;
        public String cover;
        public String catalog1 = "";
        public String catalog2 = "";
        public String catalog3 = "";
        public String catalog4 = "";
        public String catalog5 = "";
        public String catalog1Name = "";
        public String catalog2Name = "";
        public String catalog3Name = "";
        public String catalog4Name = "";
        public String catalog5Name = "";
        public Integer status = 0;
        public Integer isVisible = 0;
        public Integer count = 0;
        public String duration = "";
        public String up_Date = "";
        public String create_date = "";
        // 草稿课时数
        public Integer status0Count = 0;
        // 待审核课时数
        public Integer status1Count = 0;
        // 已通过课时数
        public Integer status2Count = 0;
        // 未通过课时数
        public Integer status3Count = 0;

        public Integer isJingpinLesson = -1;

    }

    public static class RespModel extends YxyApiRespModel {
        public List<Pojo> list;
        public Integer allPage = 0;
    }

}
